安装 趟坑
装这个版本
go get -u -v github.com/micro/go-micro@v1.8.0
高级版本已经没有 consul
启动服务发现的例子
/**
* Created by GoLand.
* User: 清行
* Contact: 66500852@qq.com
* Date: 2020/7/24
* Time: 22:02
*/
package main
import (
"github.com/gin-gonic/gin"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/consul"
"github.com/micro/go-micro/web"
)
func main() {
consulReg := consul.NewRegistry(
registry.Addrs("192.168.3.9:8500"),
)
ginRoute:=gin.Default()
ginRoute.Handle("GET","/user", func(context *gin.Context) {
context.String(200,"user api")
})
server := web.NewService(
web.Name("asdasd"),
web.Address(":8001"),
web.Handler(ginRoute),
web.Registry(consulReg),
)
server.Run()
}
执行 protoc --go_out=./ --micro_out=./ *.proto
报错
- 'protoc-gen-micro' 不是内部或外部命令,也不是可运行的程序
# 安装 protoc-gen-go
go get google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go
# 安装 protoc-gen-micro
go get github.com/micro/micro/v2/cmd/protoc-gen-micro
go install github.com/micro/micro/v2/cmd/protoc-gen-micro@latest
# V3版本的
go install github.com/asim/go-micro/cmd/protoc-gen-micro/v3@latest
etcd 与 grpc 不测试问题的
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
vendor\github.com\coreos\etcd\clientv3\balancer\resolver\endpoint\endpoint.go:114:87: undefined: resolver.BuildOption
vendor\github.com\coreos\etcd\clientv3\balancer\resolver\endpoint\endpoint.go:182:40: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/picker
vendor\github.com\coreos\etcd\clientv3\balancer\picker\err.go:37:53: undefined: balancer.PickOptions
vendor\github.com\coreos\etcd\clientv3\balancer\picker\roundrobin_balanced.go:55:63: undefined: balancer.PickOptions
解决方案: 在go.mod中将grpc的版本修改为v1.26.0
下面语句添加上去的,直接修改版本号,会遇到另外的坑
replace (
google.golang.org/grpc => google.golang.org/grpc v1.27.1
)
micro api --handler=rpc
调用 rpc 方法 micro call service Cap.SayHello